package com.gr.search;

/**
 * Copyright Manning Publications Co.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific lan
 */
import com.gr.analys.VietAnalyzer;
import java.io.*;
import java.util.Random;
import javax.swing.JOptionPane;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

// From chapter 1
/**
 * This code was originally written for Erik's Lucene intro java.net article
 */
public class NormalIndex {

    ManagerRecord manager = new ManagerRecord();
    private IndexWriter writer;

    public static void main(String[] args) throws Exception {
        /*
         * if (args.length != 2) { throw new IllegalArgumentException("Usage:
         * java " + Indexer.class.getName() + " <index dir> <data dir>"); }
         */
        //location of Index result folder
        String indexDir = "C:/Index";         //1
        //location of txt Data source folder
        String dataDir = "C:/TestData";         //2

        //start to count time of index
        long start = System.currentTimeMillis();

        NormalIndex indexer = new NormalIndex(indexDir);
        int numIndexed;
        try {
            numIndexed = indexer.index(dataDir, new TextFilesFilter());
        } finally {
            indexer.close();
        }
        //count the time of ending
        long end = System.currentTimeMillis();
        //print out the time of indexing file
        System.out.println("Indexing " + numIndexed + " files took "
                + (end - start) + " milliseconds");
    }

    //constructor for this class NormalIndex
    public NormalIndex(String indexDir) throws IOException {
        //open the directory for
        Directory dir = FSDirectory.open(new File(indexDir));
        System.out.println(dir.toString());
        writer = new IndexWriter(dir, //3
                new VietAnalyzer(),//3
                true, //3
                IndexWriter.MaxFieldLength.UNLIMITED); //3
//    writer.setInfoStream(System.out);
        //   writer.optimize();
        // writer.setUseCompoundFile(false);
    }

    //close the Indexwriter
    public void close() throws IOException {
        writer.close();                             //4
    }

    //index algorithm
    public int index(String dataDir, FileFilter filter)
            throws Exception {

        File[] files = new File(dataDir).listFiles();

        for (File f : files) {
            if (!f.isDirectory()
                    && !f.isHidden()
                    && f.exists()
                    && f.canRead()
                    && (filter == null || filter.accept(f))) {
                indexFile(f);
            }
        }
        return writer.numDocs();                     //5
    }

    //filter file type, only accept .txt files
    private static class TextFilesFilter implements FileFilter {

        public boolean accept(File path) {
            return path.getName().toLowerCase().endsWith(".txt");                  //6
        }
    }

    //process document to index
    protected Document getDocument(File f) throws Exception {
        Document doc = new Document();
        doc.add(new Field("contents", new InputStreamReader(new FileInputStream(f), "UTF-8")));      //7 
        doc.add(new Field("filename", f.getCanonicalPath(), //8 
                Field.Store.YES, Field.Index.NOT_ANALYZED));

//        int docID = manager.getLastDocID() + 1;
        int docID = 0;
        docID = getdocID()+1;
        System.out.println(docID);
        TokenStream uidTokenStream = new UIDTokenStream(docID);
        doc.add(new Field("docID", uidTokenStream));
//        Random generator = new Random();
//        float k1 = generator.nextFloat() * 359 - 180;
//        float k2 = generator.nextFloat() * 359 - 180;
//        LocationRecord lc = new LocationRecord(k1, k2, docID);
//        manager.insertLoc(lc);
        writedocID(docID);
        return doc;
    }

    //get docID from file
    private int getdocID() {
        int docID = 0;
        try {
            FileInputStream fis = new FileInputStream("D:\\tmp_doc.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            while (br.readLine()!=null) {
                docID++;
            }
            br.close();
            fis.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return docID;
    }

    //write docID into file
    private void writedocID(int docID) {
        try {
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\tmp_doc.txt", true)));
            bw.append(String.valueOf(docID)+"\n");
            bw.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
//index function
    private void indexFile(File f) throws Exception {
        System.out.println("Indexing " + f.getCanonicalPath());
        Document doc = getDocument(f);
        writer.addDocument(doc);
    }
}

/*
 * #1 Create index in this directory #2 Index *.txt files from this directory #3
 * Create Lucene IndexWriter #4 Close IndexWriter #5 Return number of documents
 * indexed #7 Index file content #8 Index file name #9 Index file full path #10
 * Add document to Lucene index
 */
